'use strict'

const {parse} = require('@babel/parser');
const traverse = require('d:/WorkSpace/js_obscure_reducation/node_modules/@babel/traverse').default;
const t = require('@babel/types');
const generator = require('d:/WorkSpace/js_obscure_reducation/node_modules/@babel/generator').default;

/**-----------------------------------------------------------------------------------------------------
 * 例一，还原十六进制字符串混淆
 * 方法：观察types构造一个StringLiteral节点required要素，观察在线解析的ast，删除extra属性
 **/
let code = `var a = "\x31\x32\x33\x34\x35\x36";`;

let ast = parse(code);

let visitor = {
    StringLiteral:{
        enter:function(path){
            delete path.node.extra;
        },
    },
}

traverse(ast, visitor);

code = generator(ast).code;
console.log(code)

/**-----------------------------------------------------------------------------------------------------
 * 例二，直接删除一个节点
 * 方法有多种，通过绑定名删除，通过值类型（节点类型）删除都可以
 **/

code = `var a = 123,b = "123";`;
ast = parse(code);

visitor = {
    VariableDeclarator:{
        enter:function(path){
            // if(path.node.id.name == 'b'){
            //     path.remove();
            // }
            if(path.get('init').isStringLiteral()){
                path.remove()
            }
        },
    },
}

traverse(ast, visitor);
console.log(generator(ast).code)

/**-----------------------------------------------------------------------------------------------------
 * 例三，使用Array对象的pop()
 * 删除crack()
 **/

code = `function test(){
    var a = 123;
    var c = a + b;
    crack();
}`;

ast = parse(code);

visitor = {
    BlockStatement:{
        enter:function(path){
            let body = path.node.body;
            body.pop();
        },
    },
}

traverse(ast, visitor);
console.log(generator(ast).code);
